package com.tuanzi.loan.core.service;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author caiwanlin
 * @create 2018-01-12 11:01
 */
public abstract class CommonServiceImpl<T,PK extends Serializable> implements CommonService<T,PK>  {

    protected abstract JpaRepository<T, PK> getRepository();

    @Override
    public T findOne(PK id) {
        return getRepository().findOne(id);
    }

    @Override
    public List<T> findAll() {
        return getRepository().findAll();
    }

    @Override
    public List<T> findAll(Sort sort) {
        return getRepository().findAll(sort);
    }

    @Override
    public T saveOrUpdate(T t) {
        return  getRepository().save(t);
    }

    @Override
    public List<T> saveOrUpdate(List<T> t) {
        return t.parallelStream().map(this::saveOrUpdate).collect(Collectors.toList());
    }

    @Override
    public List<T> findAll(List<PK> ids) {
        return getRepository().findAll(ids);
    }

    @Override
    public Boolean exists(PK id) {
        return getRepository().exists(id);
    }

    @Override
    public void delete(List<T> entities) {
        getRepository().deleteInBatch(entities);
    }

    @Override
    public void delete(T t) {
        getRepository().delete(t);
    }

    @Override
    public void delete(PK id) {
        getRepository().delete(id);
    }
}
